package com.punch.course.util;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.punch.common.enums.course.WeekEnum;
import com.punch.common.feign.FeignHolidayService;
import com.punch.common.utils.SpringUtil;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class CourseClassUtil {

    private static FeignHolidayService feignHolidayService = SpringUtil.getBean(FeignHolidayService.class);

    /**
     * 获取真正的开班日期
     *
     * @param inputLocalDate 输入的开班日期
     * @return 开班日期
     */
    public static List<LocalDate> getRealOpenClassDate(LocalDate inputLocalDate, List<Integer> courseWeekList, Integer courseCount) {
        long startTime = System.currentTimeMillis();
        int thisWeekDay = inputLocalDate.getDayOfWeek().getValue();
        List<LocalDate> weekLocalDate = new ArrayList<>();
        courseWeekList.forEach(integer -> {
            LocalDate localDate1 = inputLocalDate.plusDays(integer - thisWeekDay);
            weekLocalDate.add(localDate1);
        });
        List<LocalDate> allCourseDate = new ArrayList<>();
        //输入的开班日期在选择的周几里面
        if (courseWeekList.contains(thisWeekDay)) {
            int thisWeekDayIndex = courseWeekList.indexOf(thisWeekDay);
            for (int i = thisWeekDayIndex; i < weekLocalDate.size(); i++) {
                allCourseDate.add(weekLocalDate.get(i));
            }
        } else {
            for (int i = 0; i < weekLocalDate.size(); i++) {
                LocalDate data = weekLocalDate.get(i);
                //选择的开班时间小于列表中的日期,说明那个日期就是真正的开班日期
                if (inputLocalDate.compareTo(data) < 0) {
                    for (int j = i; j < weekLocalDate.size(); j++) {
                        allCourseDate.add(weekLocalDate.get(j));
                    }
                    break;
                }
            }
        }
        //开班日期的周上了多少节课
        int weekCourse = courseWeekList.size();
        int currentWeekCourse = allCourseDate.size();
        int leaveCourseCount = courseCount - currentWeekCourse;
        //需要完整几周
        int fullWeek = leaveCourseCount / weekCourse;
        for (int i = 1; i <= fullWeek; i++) {
            long finalI = i;
            weekLocalDate.forEach(week -> allCourseDate.add(week.plusWeeks(finalI)));
        }
        //最后一周需要还需要几天，如果为0说明 没多余的周几
        int weekCount = leaveCourseCount % weekCourse;
        if (weekCount != 0) {
            for (int i = 0; i < weekCount; i++) {
                long finalI = fullWeek + 1;
                allCourseDate.add(weekLocalDate.get(i).plusWeeks(finalI));
            }
        }
        log.info("上课周期:{}", JSONUtil.parse(allCourseDate));
        return allCourseDate;
    }

    public static Map<String, Object> getOpenDateAndCourseCount(LocalDate planOpenDate, List<Integer> courseWeekList, Integer courseCount) {
        Map<String, Object> retMap = new HashMap<>();
        List<LocalDate> realOpenClassDateList = CourseClassUtil.getRealOpenClassDate(planOpenDate, courseWeekList, courseCount);
        //查找他们之间是否有节假日
        LocalDate startDate = realOpenClassDateList.get(0);
        LocalDate endDate = realOpenClassDateList.get(realOpenClassDateList.size() - 1);
        List<LocalDate> localDateList = feignHolidayService.listHoliday(startDate, endDate);
        LocalDate actualOpenDate = null;
        Integer count = 0;
        for (LocalDate localDate : realOpenClassDateList) {
            //判断该上课日期是否是节假日
            if (!localDateList.contains(localDate)) {
                count++;
                if (actualOpenDate == null) {
                    actualOpenDate = localDate;
                }
            }
        }
        retMap.put("actualOpenDate", actualOpenDate);
        retMap.put("courseHourCount", count);
        return retMap;
    }


    /**
     * 根据周、上课时间 转换成 每周xxx、xxxx xx:xx~xx:xx
     *
     * @param courseWeek 上课周期
     * @param classTime  上课时间
     * @return string
     */
    public static String getIntWeekToString(String courseWeek, String classTime) {
        if (StrUtil.isEmpty(courseWeek) || StrUtil.isEmpty(classTime)) {
            return "";
        }
        String[] courseWeekArr = courseWeek.split(",");
        StringBuilder courseTime = new StringBuilder();
        courseTime.append("每周");
        for (String courseWeekStr : courseWeekArr) {
            courseTime.append(WeekEnum.getSimple(Integer.valueOf(courseWeekStr))).append("、");
        }
        courseTime.deleteCharAt(courseTime.length() - 1);
        courseTime.append(" ").append(classTime);
        return courseTime.toString();
    }

}
